package com.danskin.wechat.webapi;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.entity.UserInfo;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.UserService;
import com.danskin.wechat.service.VipActivityService;
import com.danskin.wechat.util.JsonUtils;

@Controller
@RequestMapping("api/sale")
public class UserLoginApiController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private VipActivityService activityService;
	
	
	@RequestMapping(value="/toLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String toLogin(@RequestBody Map<String,String> map){
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if(StringUtils.isBlank(map.get("userName"))){
			access.setErrorCode("400");
			access.setErrorMsg("用户名不能为空");
		}else
		if(StringUtils.isBlank(map.get("password"))){
			access.setErrorCode("400");
			access.setErrorMsg("密码不能为空");
		}else{
			String userName=map.get("userName");
			String password=map.get("password");
			access = userService.userLogin(userName,password);
		}
		
		
		return JsonUtils.objectToJson(access);
	}
	
	
	@RequestMapping(value="/getUserInfoByToken", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String getUserInfoByToken(@RequestBody Map<String,String> map){
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		Map<String,Object> data=new HashMap<String, Object>();
		if(StringUtils.isBlank(map.get("token"))){
			access.setErrorCode("400");
			access.setErrorMsg("签名不能为空");
		}else{
			String token = map.get("token");
			UserInfo info = userService.getUserByInfoToken(token);	
			if(StringUtils.isNotBlank(String.valueOf(info))){
				data.put("userName", info.getUserName());
				data.put("id", info.getId());
				data.put("storeId", info.getStoreId());
				data.put("headimg", info.getHeadImg());
				data.put("mobile", info.getMobile());
				access.setErrorCode("200");
				access.setErrorMsg("获取用户成功");
				access.setData(data);
			}else{
				access.setErrorCode("500");
				access.setErrorMsg("获取用户信息失败");
				access.setData(new HashMap<String, Object>());
			}
			}
		
		
		return JsonUtils.objectToJson(access);
	}
	
	
	
	/**
	 * 获取用户创建的活动列表
	 */
	
	@RequestMapping(value="/getActivityListByToken", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String getActivityListByToken(@RequestBody Map<String,String> map){
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		Map<String,Object> data=new HashMap<String, Object>();
		if(StringUtils.isBlank(map.get("token"))){
			access.setErrorCode("400");
			access.setErrorMsg("签名不能为空");
		}else{
			String token = map.get("token");
			UserInfo info = userService.getUserByInfoToken(token);	
			if(StringUtils.isNotBlank(String.valueOf(info))){
			
				
				
				
				
			}else{
				access.setErrorCode("500");
				access.setErrorMsg("获取用户信息失败");
				access.setData(new HashMap<String, Object>());
			}
			}
		
		
		return JsonUtils.objectToJson(access);
	}
	
	

}
